package com.ysg.utils;

import android.content.Context;
import android.widget.ImageView;

import androidx.appcompat.widget.AppCompatImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.ysg.utils.image.CornerTransform;
import com.ysg.widget.image.blur.BlurTransformation;

public class YImageUtil {
    public static void show(ImageView imageView, String url){
        Glide.with(imageView.getContext())
                .load(url)
//                .placeholder(R.mipmap.ic_default_avatar)
//                .error(R.mipmap.avatar_normal)
                .centerCrop()
                .into(imageView);
    }

    public static void show(AppCompatImageView imageView, String url){
        Glide.with(imageView.getContext())
                .load(url)
//                .placeholder(R.mipmap.ic_default_avatar)
//                .error(R.mipmap.avatar_normal)
                .centerCrop()
                .into(imageView);
    }

    public static void showRadius(ImageView imageView, String url){
        CornerTransform transformation = new CornerTransform(imageView.getContext(), dip2px(imageView.getContext(), 10));
        transformation.setExceptCorner(false, false, false, false);

        Glide.with(imageView.getContext())
                .asBitmap() //加载with后面，load前面
                .skipMemoryCache(true) //必须添加此属性
                .load(url)
                .thumbnail(0.2f)
                .transform(transformation)
                .into(imageView);

        // 加载为四个都是圆角的图片 可以设置圆角幅度
//        RequestOptions options = new RequestOptions()
//                .bitmapTransform(new RoundedCorners(dip2px(imageView.getContext(), 10)));
//        Glide.with(imageView.getContext()).asBitmap()
//                .load(url)
//                .apply(options)
//                .into(imageView);

    }

    /**
     * 毛玻璃效果
     */
    public static void showBlur(ImageView imageView, String url){
        Glide.with(imageView.getContext())
                .load(url)
                .apply(RequestOptions.bitmapTransform(new BlurTransformation(imageView.getContext(), 25, 3)))
                .into(imageView);
    }

    public static void show(ImageView imageView, int resId){
        Glide.with(imageView.getContext()).load(resId).into(imageView);
    }

    public static void showGif(ImageView imageView, int resId){
        Glide.with(imageView.getContext()).asGif().load(resId).into(imageView);
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
